import './detail.less'

import dictsMixin from '@/mixins/dicts'
import proposalMixin from '@/mixins/proposal'
import uploadMixin from '@/mixins/upload'
import cityMixin from '@/mixins/cities'

import { formatDate, getDay } from '@/mixins/filters'

import layer from '@/components/layer'

export default {
	mixins: [dictsMixin, proposalMixin, cityMixin, uploadMixin],
	data() {
		return {
			caseStatus: false,
			infoType: '',
			id: this.$route.query.id || 0,
			userInfo: this.$store.state.common.userInfo,
			caseDetail: {
				caseInformer: {},
				caseBeReported: {},
				caseInfo: {},

				customer: [{}],
				caseBeReported: [{}]
			},
			evidence: {
				filter: {
					caseInfoId: this.$route.query.id,
					pageBean: {
						pageIndex: 1,
						pageSize: 10
					},
				},
				data: []
			},
			record: {
				filter: {
					caseInfoId: this.$route.query.id,
					pageBean: {
						pageIndex: 1,
						pageSize: 10
					},
				},
				data: []
			},
			track: {
				filter: {
					soureId: this.$route.query.id,
					pageBean: {
						pageIndex: 1,
						pageSize: 10
					},
				},
				data: []
			},
			report: {
				filter: {
					caseInfoId: this.$route.query.id,
					pageBean: {
						pageIndex: 1,
						pageSize: 10
					},
				},
				data: []
			},
			search: {
				loading: false,
				list: [],
				timer: null
			},
			// 添加 ---（证据、报告）
			addInfoLayer: {
				url: '',
				show: false,
				title: '',
				text: '',
				form: {
					remark: '',
					caseInfoId: this.$route.query.id,
				},
				uploadUrl: api.API_BASE_URL + '/upload',
				fileList: [],
				fileNames: []
			},
			formData: {},
			isCaseUpdate: !!!this.$route.query.origin,
			// 记录初查记录是否添加了省份和城市
			provinceFlag: false,

			tabIndex: 0,
			// 举证资料
			evidenceList: [{}],
			// 回访组回访记录
			visitRecord: {},
			// 专项组操作记录
			operateRecord: [],
			// 案件调查记录
			investigateRecord: {
				layer: {
					show: false,
					title: '新增案件调查记录'
				},
				form: {},
				list: []
			},
			// 案件基本情况
			caseSituation: {},
			// 办案人员
			caseInvestigators: [],
			// 涉案人员
			peopleInvolved: [],
			// 受害人员
			victims: [],
			// 嫌疑人员
			suspects: [],
			// 案卡维度
			caseCardDimension: {
				layer: {
					show: false,
					title: '新增案卡维度'
				},
				form: {},
				list: []
			},
			// 案卷维度
			caseDimension: {
				layer: {
					show: false,
					title: '新增案卷维度'
				},
				form: {},
				list: []
			}
		}
	},
	components: {
		layer
	},
	mounted() {
		this.$store.dispatch('setBreadData', [
			{
				name: '案件详情'
			}
		])

		this.proposalTableData.data = []
		this.proposalTableData.ajax = true

		this.getCaseDetail()
		this.getEvidence()
		this.getRecord()
		this.getReport()
		this.getTrack()

		// 省份
		this.getProvince()

		this.uploadData.limit = 1

	},
	methods: {
		// 返回
		goBack() {
			this.$router.go(-1)
		},
		tabClick(i) {
			this.tabIndex = i
		},

		// 案件详情
		getCaseDetail() {
			api.caseDetail({ id: this.id }).then(res => {
				if (res.caseInformer.reflectSex != undefined) {
					res.caseInformer.reflectSex = res.caseInformer.reflectSex == 1 ? '男' : '女'
				}
				res.caseInformer.creditDate = res.caseInformer.creditDate ? formatDate(res.caseInformer.creditDate, 'yyyy-MM-dd') : ''
				res.caseInformer.borrowDate = res.caseInformer.borrowDate ? formatDate(res.caseInformer.borrowDate, 'yyyy-MM-dd') : ''
				res.caseInfo.gainCaseDate = res.caseInfo.gainCaseDate ? formatDate(res.caseInfo.gainCaseDate, 'yyyy-MM-dd') : ''

				if (res.caseInformer.managerChange != undefined) {
					res.caseInformer.managerChange = res.caseInformer.managerChange == 1 ? '是' : '否'
				}

				if (res.caseInfo.source) {
					let _obj = this.dicts.sourceType.find(o => o.value == res.caseInfo.source)
					if (_obj) {
						res.caseInfo.sourceName = _obj.name
					}
				}
				if (res.caseInfo.product) {
					let _obj = this.dicts.productType.find(o => o.value == res.caseInfo.product)
					if (_obj) {
						res.caseInfo.productName = _obj.name
					}
				}
				if (res.caseInfo.caseType) {
					let _obj = this.dicts.caseType.find(o => o.value == res.caseInfo.caseType)
					if (_obj) {
						res.caseInfo.caseTypeName = _obj.name
					}
				}
				if (res.caseInfo.province) {
					this.provinces.form.id = res.caseInfo.province
					this.provinceFlag = true
				}
				if (res.caseInfo.city) {
					this.cities.form.name = res.caseInfo.city
					this.provinceFlag = true
				}

				if (res.caseRiskProposalList) {
					res.caseRiskProposalList.map(item => {
						if (item.cdate) {
							item.cdate = formatDate(item.cdate)
						}
						if (item.riskType) {
							let _obj = this.dicts.riskType.find(o => o.value == item.riskType)
							if (_obj) {
								item.riskTypeName = _obj.name
							}
						}
						if (item.riskCategory) {
							let _obj = this.dicts.riskCategory.find(o => o.value == item.riskCategory)
							if (_obj) {
								item.riskCategoryName = _obj.name
							}
						}
						if (item.riskLevel) {
							let _obj = this.dicts.riskLevel.find(o => o.value == item.riskLevel)
							if (_obj) {
								item.riskLevelName = _obj.name
							}
						}
						if (item.riskDisposeDate) {
							let _obj = this.dicts.riskDisposeDate.find(o => o.value == item.riskDisposeDate)
							if (_obj) {
								item.riskDisposeDateName = _obj.name
							}
						}
						item.riskDetailsName = item.riskDetails
						return item
					})
					this.proposalTableData.data = res.caseRiskProposalList
					this.proposalTableData.total = res.caseRiskProposalList.length
				}

				this.caseDetail = res
				this.caseDetail.caseInformer.setSalesDept = this.caseDetail.caseInformer.salesDeptCode

				this.caseDetail.customer = [{}]
				this.caseDetail.caseBeReported = [{}]

				this.caseStatus = this.caseDetail.caseInfo.caseStatus == 1 || this.caseDetail.caseInfo.caseStatus == 3

				this.proposalTableData.type = 2
				this.getProposalData(1)
			}).catch(res => {
				console.log(res)
			})
		},

		// 证据 -- 分页
		evidenceCurrentChange(val) {
			this.evidence.filter.pageBean.pageIndex = val
			this.getEvidence()
		},
		// 证据 -- 条数改变
		evidenceSizeChange(val) {
			this.evidence.filter.pageBean.pageIndex = 1;
			this.evidence.filter.pageBean.pageSize = val;
			this.getEvidence()
		},
		// 获取证据
		getEvidence() {
			api.caseEvidenceList(this.evidence.filter).then(res => {
				// this.evidence.total = res.totalCount
				this.evidenceList = res.list
			}).catch(err => {
				console.log(err)
			})
		},

		// 调查记录 -- 分页
		recordCurrentChange(val) {
			this.record.filter.pageBean.pageIndex = val;
			this.getRecord();
		},
		// 调查记录 -- 条数改变
		recordSizeChange(val) {
			this.record.filter.pageBean.pageIndex = 1;
			this.record.filter.pageBean.pageSize = val;
			this.getRecord();
		},
		// 获取调查记录
		getRecord() {
			api.caseRecordList(this.record.filter).then(res => {
				this.record.total = res.totalCount;
				this.record.data = res.list;
			}, error => { })
		},
		// 报告 -- 分页
		reportCurrentChange(val) {
			this.report.filter.pageBean.pageIndex = val;
			this.getReport();
		},
		// 报告 -- 条数改变
		reportSizeChange(val) {
			this.report.filter.pageBean.pageIndex = 1;
			this.report.filter.pageBean.pageSize = val;
			this.getReport();
		},
		// 报告 -- 分页
		trackCurrentChange(val) {
			this.track.filter.pageBean.pageIndex = val;
			this.getTrack();
		},
		// 报告 -- 条数改变
		trackSizeChange(val) {
			this.track.filter.pageBean.pageIndex = 1;
			this.track.filter.pageBean.pageSize = val;
			this.getTrack();
		},
		// 获取轨迹
		getTrack() {
			api.caseTrackList(this.track.filter).then(res => {
				this.track.total = res.totalCount;
				this.track.data = res.list;
			}, error => { })
		},
		// 获取报告
		getReport() {
			api.caseReportList(this.report.filter).then(res => {
				this.report.total = res.totalCount;
				this.report.data = res.list;
			}, error => { })
		},
		// 添加证据 -- 添加报告 -- 弹窗
		showInfoLayer(type) {
			this.addInfoLayer.form = {};
			this.addInfoLayer.fileList = []

			this.infoType = type;
			// 证据
			if (type === 'caseEvidences') {
				this.addInfoLayer.text = '上传证据文件'
				this.addInfoLayer.title = '添加证据'
				this.addInfoLayer.url = 'addCaseEvidence'
			} else if (type === 'caseReports') {
				// 报告
				this.addInfoLayer.text = '上传报告文件'
				this.addInfoLayer.title = '添加报告'
				this.addInfoLayer.url = 'addCaseReport'
			} else if (type === 'caseRecords') {
				// 调查记录
				this.addInfoLayer.text = '上传调查附件'
				this.addInfoLayer.title = '添加调查记录'
				this.addInfoLayer.url = 'addCaseRecord'
			}

			this.addInfoLayer.show = true;
		},
		// 删除证据
		delEvidence(id) {
			api.deleteCaseEvidence({ id: id }).then(res => {
				this.$msg.success('删除成功');
				this.getEvidence();
			}).catch(res => {
				console.log(res)
			})
		},
		// 删除报告
		delReport(id) {
			api.deleteCaseReport({ id: id }).then(res => {
				this.$msg.success('删除成功');
				this.getReport()
			}).catch(res => {
				console.log(res)
			})
		},
		// 提交申请结案
		infoSave() {
			// if (this.addInfoLayer.fileList && !this.addInfoLayer.fileList.length) {
			// 	this.$msg.error('请' + this.addInfoLayer.text)
			// 	return false
			// }
			let _data = {}
			_data[this.infoType] = []
			if (this.addInfoLayer.fileList && !this.addInfoLayer.fileList.length) {
				let _obj = {}
				_obj.caseInfoId = this.id
				if (this.addInfoLayer.form.remark) {
					_obj.remark = this.addInfoLayer.form.remark
				}
				if (this.addInfoLayer.form.content) {
					_obj.content = this.addInfoLayer.form.content
				}
				_data[this.infoType].push(_obj)
			}
			else {
				this.addInfoLayer.fileList.forEach(item => {
					let _obj = {}
					_obj.fileName = item.name
					_obj.fileUrl = item.url
					_obj.caseInfoId = this.id
					if (this.addInfoLayer.form.remark) {
						_obj.remark = this.addInfoLayer.form.remark
					}
					if (this.addInfoLayer.form.content) {
						_obj.content = this.addInfoLayer.form.content
					}
					_data[this.infoType].push(_obj)
				})
			}

			api[this.addInfoLayer.url](_data).then(res => {
				this.$msg.success('添加成功')
				this.addInfoLayer.show = false
				if (this.infoType === 'caseEvidences') {
					this.getEvidence();
				} else if (this.infoType === 'caseReports') {
					// 报告
					this.getReport();
				} else if (this.infoType === 'caseRecords') {
					// 调查记录
					this.getRecord();
				}
			}).catch(res => {
				console.log(res)
			})
		},
		//下载文件
		downloadFile(url) {
			let splitUrl = url.split("downLoad?")[1]
			let splitPath = splitUrl.split('path=')[1]
			let path = splitPath.split('&fileName=')[0];
			let fileName = splitPath.split('&fileName=')[1];

			api.download({ path: path, fileName: fileName }).then(res => {
				let $a = document.createElement('a');
				$a.setAttribute('href', res.base64);
				$a.setAttribute('download', res.fileName);
				$a.setAttribute('target', '_blank');
				$a.click();
			}).catch(res => {
				console.log(res)
			})
		},
		// 修改案件
		editCaseInfoVo(type) {
			if (this.provinceFlag) {
				return false
			}
			if (!(this.provinces.form.id || this.cities.form.name)) {
				return false
			}
			let _data = {}
			_data.caseInfo = {}
			_data.caseInfo.id = this.caseDetail.caseInfo.id
			let _p = this.provinces.list.find(item => item.id == this.provinces.form.id)
			_data.caseInfo.province = _p.name
			_data.caseInfo.city = this.cities.form.name
			api.editCaseInfoVo(_data)
				.then(res => {
					if (type) {
						this.$msg.success('保存成功')
					}
					this.provinceFlag = true
				})
				.catch(res => {
					console.log(res)
				})
		},

		// 上传文件之前验证格式
		beforeUpload(file) {
			if (file.size == 0) {
				this.$msg.error('上传文件大小不能为0B!')
				return false
			}
			// // const isIamge = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif';
			// const limitSize = file.size / 1024 / 1024 < 100;

			// // if (!isIamge) {
			// //     this.$msg.error('上传文件只能是 JPG png gif格式!');
			// // }
			// if (!limitSize) {
			// 	this.$msg.error('上传文件大小不能超过 100MB!');
			// }
			// // return isIamge && limitSize;
			// return limitSize;
		},
		// 文件列表移除文件时的钩子
		removeUploadFile(file, fileList) {
			this.addInfoLayer.fileList = fileList
		},
		// 上传文件成功
		uploadSuccess(res, file) {
			var _res = file.response;
			if (_res.code == 10000) {
				let _obj = {}
				_obj.name = file.name
				_obj.url = _res.data.url
				this.addInfoLayer.fileList.push(_obj)
			}
			else {
				this.$msg.error(_res.msg);
			}
		},

		dateFilter(date) {
			return formatDate(date)
		},
		getDayFilter(date) {
			return getDay(date)
		},

		// 案件调查记录弹窗
		showInvestigateLayer() {
			this.investigateRecord.layer.show = true
			this.investigateRecord.form = {}
			this.uploadData.fileList = []
		},
		// 新增案件调查记录
		addInvestigateRecord() {
			this.investigateRecord.form.creator = this.userInfo.name
			this.investigateRecord.form.cdate = formatDate(Date.now())
			if (this.uploadData.fileList.length) {
				this.investigateRecord.form.fileName = this.uploadData.fileList[0].name
			}

			this.investigateRecord.list.push(this.investigateRecord.form)
			this.investigateRecord.layer.show = false
		},
		// 删除案件调查记录
		deleteInvestigateRecord(i) {
			this.investigateRecord.list.splice(i, 1)
		},

		// 新增办案人员
		addInvestigator() {
			let _obj = {}
			_obj.creator = this.userInfo.name
			_obj.cdate = formatDate(Date.now())
			this.caseInvestigators.push(_obj)
		},
		// 删除办案人员
		deleteInvestigator(i) {
			this.caseInvestigators.splice(i, 1)
		},
		// 新增涉案人员
		addInvolved() {
			let _obj = {}
			_obj.creator = this.userInfo.name
			_obj.cdate = formatDate(Date.now())
			this.peopleInvolved.push(_obj)
		},
		// 删除涉案人员
		deleteInvolved(i) {
			this.peopleInvolved.splice(i, 1)
		},
		// 新增受害人员
		addVictim() {
			let _obj = {}
			_obj.creator = this.userInfo.name
			_obj.cdate = formatDate(Date.now())
			this.victims.push(_obj)
		},
		// 删除受害人员
		deleteVictim(i) {
			this.victims.splice(i, 1)
		},
		// 新增嫌疑人员
		addSuspect() {
			let _obj = {}
			_obj.creator = this.userInfo.name
			_obj.cdate = formatDate(Date.now())
			this.suspects.push(_obj)
		},
		// 删除嫌疑人员
		deleteSuspect(i) {
			this.suspects.splice(i, 1)
		},

		// 案卡维度弹窗
		showCaseCardDimensionLayer() {
			this.caseCardDimension.layer.show = true
			this.caseCardDimension.form = {}
			this.uploadData.fileList = []
		},
		// 新增案卡维度
		addCaseCardDimension() {
			this.caseCardDimension.form.creator = this.userInfo.name
			this.caseCardDimension.form.cdate = formatDate(Date.now())
			if (this.uploadData.fileList.length) {
				this.caseCardDimension.form.fileName = this.uploadData.fileList[0].name
			}

			this.caseCardDimension.list.push(this.caseCardDimension.form)
			this.caseCardDimension.layer.show = false
		},
		// 删除案卡维度
		deleteCaseCardDimension(i) {
			this.caseCardDimension.list.splice(i, 1)
		},

		// 案卷维度弹窗
		showCaseDimensionLayer() {
			this.caseDimension.layer.show = true
			this.caseDimension.form = {}
			this.uploadData.fileList = []
		},
		// 新增案卷维度
		addCaseDimension() {
			this.caseDimension.form.creator = this.userInfo.name
			this.caseDimension.form.cdate = formatDate(Date.now())
			if (this.uploadData.fileList.length) {
				this.caseDimension.form.fileName = this.uploadData.fileList[0].name
			}

			this.caseDimension.list.push(this.caseDimension.form)
			this.caseDimension.layer.show = false
		},
		// 删除案卷维度
		deleteCaseDimension(i) {
			this.caseDimension.list.splice(i, 1)
		}
	}
}